package com.zmc.third.infrastructure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmc.common.utils.PageUtils;
import com.zmc.common.utils.Query;
import com.zmc.third.infrastructure.dao.PaymentInfoDao;
import com.zmc.third.infrastructure.entity.PaymentInfoEntity;
import com.zmc.third.infrastructure.enums.PayEnum;
import com.zmc.third.infrastructure.service.PaymentInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Map;


/**
 * @author zmc
 */
@Service("paymentInfoService")
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoDao, PaymentInfoEntity> implements PaymentInfoService {


    @Override
    public PaymentInfoEntity getByOrderSn(String orderSn) {
        LambdaQueryWrapper<PaymentInfoEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PaymentInfoEntity::getOrderSn, orderSn);
        return getOne(wrapper);
    }

    @Override
    public PaymentInfoEntity getByTradeSn(String outTradeNo) {
        LambdaQueryWrapper<PaymentInfoEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PaymentInfoEntity::getTradeSn, outTradeNo);
        return getOne(wrapper);
    }

    @Override
    @Transactional
    public void paySuccessUpdateInfo(String tradeSn, String alipayTradeSn, String callbackContent) {
        LambdaUpdateWrapper<PaymentInfoEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(PaymentInfoEntity::getTradeSn, tradeSn)
                .set(PaymentInfoEntity::getAlipayTradeNo, alipayTradeSn)
                .set(PaymentInfoEntity::getPaymentStatus, PayEnum.PAY_SUCCESS.getCode())
                .set(PaymentInfoEntity::getCallbackContent, callbackContent)
                .set(PaymentInfoEntity::getCallbackTime, LocalDateTime.now());
        update(wrapper);
    }
}